<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-divider/>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="营销配置"
        >
          <a-select mode="multiple"
                    v-decorator="[ 'configIds', {rules: [],initialValue: config} ]">
            <a-select-option v-for="item in defaultConfig" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { saveConfig, selectAll, selectConfigByCompanyId } from '@/api/system/config'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        isDisabled: false,
        companyId: '',
        defaultConfig: [],
        config: []
      }
    },
    //--############  data() ###############-->

    created() {

    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.isDisabled = true
        this.visible = true
        this.companyId = record.id
        selectAll(record.id).then(res => {
          this.defaultConfig = res.result
        })
        selectConfigByCompanyId(record.id).then(res => {
          if (res.result != null) {
            this.config = res.result
          }
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            console.log('form values', values)
            values.id = _this.companyId
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            saveConfig(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
